package com.swinginwind.flexutils.components.dataGridClasses
{
	/**
	 * @author jimmy
	 * @E-mail: swinginwind@gmail.com
	 * @version 1.0.0
	 * 创建时间：2013-5-13 上午10:20:32
	 * 
	 */
	import com.swinginwind.flexutils.components.CenterCheckBox;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.controls.DataGrid;

	public class CheckBoxItemRenderer extends CenterCheckBox
	{
		public function CheckBoxItemRenderer()
		{
			super();
			this.addEventListener("click", mouseClickHandler);
			
		}
		/*override public function validateProperties():void
		{
			super.validateProperties();
			if (listData)
			{
				
				var dg:DataGrid = DataGrid(listData.owner);
				
				var column:CheckBoxHeaderColumn = dg.columns[listData.columnIndex];
				column.removeEventListener("click",columnHeaderClickHandler);
				column.addEventListener("click",columnHeaderClickHandler);
				selected = data[column.dataField];
			}
		}
		*/
		override public function set data(value:Object):void
		{
			super.data = value;
			if (listData)
			{
				var dg:DataGrid = DataGrid(listData.owner);
				var column:CheckBoxHeaderColumn = dg.columns[listData.columnIndex];
				column.removeEventListener("click",columnHeaderClickHandler);
				column.addEventListener("click",columnHeaderClickHandler);
				selected = data[column.dataField];
				BindingUtils.bindProperty(this, "enabled", column, "enable");
			}
			
		}
		
		
		private function columnHeaderClickHandler(event:MouseEvent):void
		{
			//why this alery shows three times for a data of 2 rows
			//mx.controls.Alert.show("alert");
			
			var dg:DataGrid = DataGrid(listData.owner);
			var column:CheckBoxHeaderColumn = dg.columns[listData.columnIndex];
			if(column.enable)
			{
				selected = event.target.selected;
				data[column.dataField] = selected;
			}
		}
		
		private function mouseClickHandler(event:MouseEvent):void
		{
			var dg:DataGrid = DataGrid(listData.owner);
			var column:CheckBoxHeaderColumn = dg.columns[listData.columnIndex];
			if(column.enable)
				data[column.dataField] = selected;
		}
		
		
		
	}
}